﻿using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System;

using Tools.Json;

using SY.WebSite.BLL;
using SY.Uap.Entity;
using SY.WebSite.Entity;

namespace SY.WebSite.Web.Handler
{
    public class IntegrationManageHandler : AdminWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action)
            {
                case "integration":
                    GetIntegrationList();
                    break;
                case "grantIntegration":
                    GrantIntegration();
                    break;

                case "transaction":
                    Transaction();
                    break;
            }

            context.Response.End();
        }

        public void Transaction() {
            string month = RequestStringParam("month");
            DateTime date;
            if (!DateTime.TryParse(month, out date)) {
                BreakResponse();
            }
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 20);
            
            IEnumerable<IntegrationTransaction> list = BizIntegrationTransaction.QueryAllTransfer(date);
            JsonTransfer jt = new JsonTransfer();
            jt.Add("count", list.Count());
            jt.Add("list", list.Skip(pageIndex * pageSize).Take(pageSize));
            Response.Write(jt);
        }

        public void GrantIntegration()
        {
            int userId = Tools.Web.Request.GetFormInt("userId", 0);
            int type = Tools.Web.Request.GetFormInt("type", 0);
            int amount = Tools.Web.Request.GetFormInt("amount", 0);
            string description = Tools.Web.Request.GetFormString("description");

            if (userId <= 0 || amount <= 0)
                BreakResponse();

            User user = BizUser.GetByKey(userId);
            if (user == null)
                BreakResponse();

            Integration integration = BizIntegration.GetByKey(userId);
            switch (type) { 
                case 1:
                    integration.Available += amount;
                    break;
                case 2:
                    integration.Available -= amount;
                    break;
                case 3:
                    integration.Honor += amount;
                    break;
                case 4:
                    integration.Honor -= amount;
                    break;
                default:
                    BreakResponse();
                    return;
            }

            int result = BizIntegration.Update(integration);
            if (result > 0)
            {
                if (type <= 2)
                {
                    IntegrationTransaction transaction = new IntegrationTransaction
                        {
                            ToUserId = userId,
                            ToUserName = user.UserName,
                            Comment = description,
                            Amount = amount,
                            Type = type
                        };
                    BizIntegrationTransaction.Add(transaction);
                }
                //todo: 系统操作日志


                Response.Write(new JsonTransfer().Add("success", ""));
                return;
            }

            BreakResponse("系统异常，请稍后再试");
        }

        public void GetIntegrationList()
        {
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 20);

            string userName = RequestStringParam("user");
            string deptName = RequestStringParam("deptName");
            string jobName = RequestStringParam("jobName");
            int gender = RequestIntParam("gender", 100);
            int userType = RequestIntParam("usertype", 200);

            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Hashtable> list = BizIntegration.GetAllIntegration();

            if (!string.IsNullOrEmpty(userName))
                list = list.Where(l => l["UserName"].ToString().Contains(userName) || l["JobNumber"].ToString().Contains(userName));

            if (!string.IsNullOrEmpty(deptName))
                list = list.Where(l => l["DeptName"].ToString().Contains(deptName));

            if (!string.IsNullOrEmpty(jobName))
                list = list.Where(l => l["JobName"].ToString().Contains(jobName));
            
            if (gender != 100)
                list = list.Where(l => int.Parse(l["Gender"].ToString()) == gender);

            if (userType != 200)
                list = list.Where(l => int.Parse(l["UserType"].ToString()) == userType);

            jt.Add("list", list.Skip(pageIndex * pageSize).Take(pageSize));
            jt.Add("count", list.Count());
            Response.Write(jt);
        }
    }
}